package com.example.mqtt.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;

@Configuration
@Slf4j
public class CurrentStatusEvent {

    @Bean
    public MessageChannel currentStatusChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "currentStatusChannel")
    public MessageHandler currentStatusHandler() {
        return message -> {
            MessageHeaders headers = message.getHeaders();
            log.info("====消息头：{}", headers);
            log.info("====主题：{}", headers.get(MqttHeaders.RECEIVED_TOPIC));

            Object payload = message.getPayload();
            log.info("====消息体：{}", payload);
        };
    }

}
